package cn.tedu.study.notemodel.controller;


import cn.tedu.study.notemodel.mapper.NoteMapper;
import cn.tedu.study.notemodel.pojo.dto.NotePostDTO;
import cn.tedu.study.notemodel.pojo.entity.Note;
import cn.tedu.study.notemodel.pojo.vo.ContentManagementVO;
import cn.tedu.study.notemodel.pojo.vo.NoteEditVO;
import cn.tedu.study.response.ResultVO;
import cn.tedu.study.response.StatusCode;
import cn.tedu.study.security.CustomUserDetail;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/notes/")
public class NoteController {
    @Value("${filePath}")
    private String filePath;

    @Autowired
    private NoteMapper noteMapper;

    @RequestMapping("insert")
    public ResultVO postNote(@RequestBody NotePostDTO notePostDTO,
                             @AuthenticationPrincipal CustomUserDetail customUserDetail) {
        Note note = new Note();
        BeanUtils.copyProperties(notePostDTO, note);
        if (notePostDTO.getId() == null) {
            note.setCreateTime(new Date());
            note.setUserId(customUserDetail.getId());
            noteMapper.insert(note);
        } else {
            note.setUpdateTime(new Date());
            note.setUserId(customUserDetail.getId());
            noteMapper.update(note);
        }
        return ResultVO.ok();
    }

    @RequestMapping("selectContent")
    public ResultVO selectContent(@AuthenticationPrincipal CustomUserDetail customUserDetail){
        if (customUserDetail==null){
            return new ResultVO(StatusCode.NOT_LOGIN);
        }

        List<ContentManagementVO> list = noteMapper.selectContent(customUserDetail.getId());
        System.out.println(customUserDetail.getId());

        return ResultVO.ok(list);
    }

    @RequestMapping("editById/{id}")
    public ResultVO editById(@PathVariable Integer id){
        NoteEditVO noteEditVO = noteMapper.editById(id);
        return ResultVO.ok(noteEditVO);
    }

    @RequestMapping("deleteById/{id}")
    public ResultVO deleteById(@PathVariable Integer id){
        noteMapper.deleteById(id);
        return ResultVO.ok();
    }
}
